﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WXYW.BLL;
using WXYW.DAL;
using System.Web.Configuration;

namespace WXYW
{
    public partial class TFSQDefine : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindUserList();
                FillFormData();
            }
        }

        private void FillFormData()
        {
            long applicationID = Convert.ToInt64(Request.QueryString[Constants.QUERYSTR_APPLICATION_ID]);
            TFSQService service = new TFSQService();
            SpecialServiceForm form = service.GetSpecialServiceForm(applicationID);
            tfsqNOLbl.Text = form.ApplicationCode;
            rmaNOLbl.Text = form.ApplicationForm.ApplicationCode;
            tfsqDate.Text = form.ApplicationDate.HasValue? form.ApplicationDate.Value.ToString("yyyy-MM-dd"):"";
            customerLbl.Text = form.ApplicationForm.User.FullName;
            contactPersonLbl.Text = form.ApplicationForm.User.ContactPerson;
            contactPhoneLbl.Text = form.ApplicationForm.User.UserMobilePhone;
            if (form.User != null)            
            {
                SQRYEmailTxtBox.Text = form.User.ContactPerson;
                SQRYID.Value = form.User.UserID.ToString();
            }
            if (form.User1 != null)
            {
                XSSYManagerEmailTxtBox.Text = form.User1.ContactPerson;
                XSManagerID.Value = form.User1.UserID.ToString();
            }
            if (form.User2 != null)
            {
                FWBMManagerEmailTxtBox.Text = form.User2.ContactPerson;
                FWManagerID.Value = form.User2.UserID.ToString();
            }
            if (form.User3 != null)
            {
                KFSYBManagerEMailTxtBox.Text = form.User3.ContactPerson;
                KFManagerID.Value = form.User3.UserID.ToString();
            }
            applicationProductGridView.DataSource = service.GetTFSQProductList(applicationID);
            applicationProductGridView.DataBind();
        }

        private void BindUserList()
        {
            UserService service = new UserService();
            UserGridView.DataSource = service.GetUserModelsByKeywork(filterTxtBox.Text);
            UserGridView.DataBind();
        }

        protected void SelectSQRLnkBtn_Click(object sender, EventArgs e)
        {
            FirePopupControl.Value = "SQRY";
            ChoosePersonPopupExtender.Show();
        }

        protected void SelectXSSYManagerLnkBtn_Click(object sender, EventArgs e)
        {
            FirePopupControl.Value = "XSManager";
            ChoosePersonPopupExtender.Show();    
        }

        protected void SelectFWBMManagerLnkBtn_Click(object sender, EventArgs e)
        {
            FirePopupControl.Value = "FWManager";
            ChoosePersonPopupExtender.Show();
        }

        protected void SelectKFSYManagerLnkBtn_Click(object sender, EventArgs e)
        {
            FirePopupControl.Value = "KFManager";
            ChoosePersonPopupExtender.Show();
        }

        protected void okLinkButton_Click(object sender, EventArgs e)
        {
            long applicationID = Convert.ToInt64(Request.QueryString[Constants.QUERYSTR_APPLICATION_ID]);
            TFSQService service = new TFSQService();
            SpecialServiceForm form = service.GetSpecialServiceForm(applicationID);
            if (form.User != null &&
                form.User1 != null &&
                form.User2 != null &&
                form.User3 != null)
            {
                service.UpdateFormStatus("2", applicationID);

                //发送邮件给申请者填写申请表
                EmailService.SendEmail(form.User.UserEmail, WebConfigurationManager.AppSettings["TFSQFillNoticeSubjct"],
                    string.Format(WebConfigurationManager.AppSettings["TFSQFillNoticeBody"], form.User.ContactPerson, form.ApplicationCode));

                Response.Write(string.Format("<script language='JavaScript'> alert( '记录已成功保存！');location.href= 'TFSQDefineList.aspx' </script>"));
            }
        }

        protected void UserGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            if (e.NewPageIndex >= 0 && e.NewPageIndex < UserGridView.PageCount)
            {
                UserGridView.PageIndex = e.NewPageIndex;
            }

            BindUserList();
            ChoosePersonPopupExtender.Show();
        }

        protected void UserGridView_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.Equals("SelectPerson"))
            {
                int userID = Convert.ToInt32(e.CommandArgument);
                long applicationID = Convert.ToInt64(Request.QueryString[Constants.QUERYSTR_APPLICATION_ID]);
                UserService service = new UserService();
                WXYW.DAL.User user = service.GetUserByID(userID);
                TFSQService tfsqService = new TFSQService();
                switch (FirePopupControl.Value)
                {
                    case "SQRY":
                        SQRYEmailTxtBox.Text = user.ContactPerson;
                        SQRYID.Value = user.UserID.ToString();
                        tfsqService.SetUser(user, applicationID);
                        break;
                    case "XSManager":
                        XSSYManagerEmailTxtBox.Text = user.ContactPerson;
                        XSManagerID.Value = user.UserID.ToString();
                        tfsqService.SetUser1(user, applicationID);
                        break;
                    case "FWManager":
                        FWBMManagerEmailTxtBox.Text = user.ContactPerson;
                        FWManagerID.Value = user.UserID.ToString();
                        tfsqService.SetUser2(user, applicationID);
                        break;
                    case "KFManager":
                        KFSYBManagerEMailTxtBox.Text = user.ContactPerson;
                        KFManagerID.Value = user.UserID.ToString();
                        tfsqService.SetUser3(user, applicationID);
                        break;
                }
            }
        }

        protected void filterBtn_Click(object sender, EventArgs e)
        {
            //if (!string.IsNullOrEmpty(filterTxtBox.Text))
            //{
                BindUserList();
                ChoosePersonPopupExtender.Show();
            //}
        }

        protected void applicationProductGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.DataItem != null)
            {
                e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#FFFF99'");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
                ((Label)e.Row.FindControl("IDLbl")).Text = (e.Row.RowIndex + 1).ToString();
            }
        }
    }
}
